package com.freeLearning.boot.system.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.freeLearning.boot.common.annotation.Log;
import com.freeLearning.boot.common.enums.LogModuleEnum;
import com.freeLearning.boot.common.enums.StatusEnum;
import com.freeLearning.boot.common.result.Result;
import com.freeLearning.boot.system.model.entity.Carousel;
import com.freeLearning.boot.system.service.CarouselService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "21.轮播图配置")
@RequestMapping("/api/v1/carousel")
public class CarouselController{

    private final CarouselService carouselService;

    @GetMapping("/allList")
    @Operation(summary = "获取轮播图列表")
    @Log( value = "系统配置分页列表",module = LogModuleEnum.CAROUSEL)
    public Result<List<Carousel>> getCarouselList() {
        return Result.success(carouselService.list());
    }

    @PostMapping("/add")
    @Operation(summary = "新增轮播图")
    @Log( value = "新增轮播图",module = LogModuleEnum.CAROUSEL)
    public Result<Boolean> addCarousel(@RequestBody Carousel carousel) {
        boolean save = carouselService.save(carousel);
        return Result.success(save);
    }

    @PostMapping("/edit")
    @Operation(summary = "修改轮播图")
    @Log( value = "修改轮播图",module = LogModuleEnum.CAROUSEL)
    @CacheEvict(value = "carousel",allEntries = true)
    public Result<Boolean> editCarousel(@RequestBody Carousel carousel) {
        boolean update = carouselService.updateById(carousel);
        return Result.success(update);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除轮播图")
    @Log( value = "删除轮播图",module = LogModuleEnum.CAROUSEL)
    @CacheEvict(value = "carousel",allEntries = true)
    public Result<Boolean> deleteCarousel(@RequestBody Carousel carousel) {
        boolean delete = carouselService.removeById(carousel.getId());
        return Result.success(delete);
    }

    @PostMapping("/active")
    @Operation(summary = "启用轮播图")
    @Log( value = "启用轮播图",module = LogModuleEnum.CAROUSEL)
    @CacheEvict(value = "carousel",allEntries = true)
    public Result<Boolean> activeCarousel(@RequestBody Carousel carousel) {
        boolean update = carouselService.update(new UpdateWrapper<Carousel>()
                .eq("id", carousel.getId())
                .set("is_active", carousel.getIsActive().equals(StatusEnum.ENABLE.getValue()) ? StatusEnum.DISABLE.getValue() : StatusEnum.ENABLE.getValue()));
        return Result.success(update);
    }

    @GetMapping("/queryActiveList")
    @Operation(summary = "获取启用的轮播图列表")
    @Log( value = "获取启用的轮播图列表",module = LogModuleEnum.CAROUSEL)
    @Cacheable(value = "carousel", key = "3",sync = true)
    public Result<List<Carousel>> getActiveCarouselList() {
        return Result.success(carouselService.list(new QueryWrapper<Carousel>()
                .eq("is_active", StatusEnum.ENABLE.getValue()).orderByAsc("sort_order")));
    }
}
